<?php


namespace Attendance\Controller;

use Think\Controller;


class IndexController extends Controller
{
    public function index()
    {   
        //查询该老师登录班级的所有学生
        $con['group_id'] = 5;
        $con['class_id'] = $_SESSION['oc_home']['class'];
        $con['unit_id']  = $_SESSION['oc_home']['unit'];
        $students   = M('core_userinfo_view')->where($con)->select();

        //拿出学生的名字
        $sName   = M('core_userinfo_view')->where($con) ->getField('nickname',true);

        //查询请假表里的学生名
        $ae   = M('core_leave')->getField('uid',true);
        //查询用户中请假的用户
        foreach ($students as $key => $value) {
            //半小时过期和三个月过期判断
            $lcon = "uid = ".$value['uid']." and unix_timestamp(now()) >= leave_date and leave_date+days*86400 >= unix_timestamp(now())";
            
            $queQin = M('core_leave')->where($lcon)->find();
            $students[$key]['type'] = $result['type'];
            $this->assign('queQin ',$queQin);
        }
            $this->assign('typelist',$students);
            $this->assign('sumcount',count($students));
            $this->display();
    }   

    public function add()
    {
        //$_SESSION['oc_home']['astate']判断考勤是否是重复考勤的变量

        //设置编码格式
        header("Content-type: text/html; charset=utf-8");
        
        //如果当天老师重复考勤，则删除当天上一次的考勤记录
        $map['aid'] = $_SESSION['oc_home']['astate'];
        $ctime = date('Y-m-d',time());
        $map['create_time'] = array(between,array(strtotime($ctime),strtotime($ctime.'23:59:58')));
        //获取学生出勤状态
        $statu               = I('names');
        //把数组元素组合为字符串
        $tmp                 = implode(',',I('names')); 
        //substr_count()判断1在$tmp出现的次数及来的人数
        $daoDeRenShu         = substr_count($tmp,'1');
        //获取总条数，转换成int类型
        $zongRenShu          = (int)I('sumcount');

        $data['unit']        = I('unit');                     //获取学校id
        $data['uid']         = $_SESSION['oc_home']['uid'];   //获取老师id
        $data['class_id']    = $_SESSION['oc_home']['class']; //获取班级id
        $data['due_num']     = $zongRenShu;                   //来的学生人数
        $data['miss_num']    = $zongRenShu-$daoDeRenShu;      //未到学生人数
        $data['create_time'] = NOW_TIME;                      //获取当前时间
        $data['status']      = 1;
        if($_SESSION['oc_home']['astate']){
            M('core_attendance_detail')->where($map)->delete();
            M('core_attendance')->where('id = '.$_SESSION['oc_home']['astate'])->save($data);
        }else{
            $result = M('core_attendance')->add($data);//addALL()数据批量增加到数据库中
            $_SESSION['oc_home']['astate'] = $result;
        }
        foreach ($_POST['asd'] as $key => $value){
            // explode()把字符串切成数组。implode（）把数组切成字符串。
            //if($statu[$key] != 1){   //判断状态不等于1的所有学生，保存到考勤表中。
                $a[] =array(
                    'aid'=>$_SESSION['oc_home']['astate'],
                    'uid'=>explode(',',$value)[0],
                    'unit'=>$_SESSION['oc_home']['unit'],
                    'class_id'=>$_SESSION['oc_home']['class'],
                    'status'=>$statu[$key],
                    'create_time'=>NOW_TIME,
                    'month' => date("Y-n")
                    );
            //}
        }
        //addALL()数据批量增加到数据库中
        $result = M('core_attendance_detail')->addAll($a);
        echo "<script>";
        echo"alert('考勤成功');";
        echo "window.location.href='./index.php?s=/Attendance/Index/index'; ";
        echo"</script>";
    }


    //园长-考勤汇总
    public function Summary()
    {
        header("Content-type: text/html; charset=utf-8");

        if(I('nickname')){  
            $map['nickname'] = array('like','%'.I('nickname').'%');
            $this->assign('nickname',I('nickname'));
        }
        if(I('createtime')&&I('endDate')){
            $map['create_time'] = array(between,array(strtotime(I('createtime')),strtotime(I('endDate'))));
            $this->assign('createtime',I('createtime'));
            $this->assign('endDate',I('endDate'));
        }
        
        switch ($_SESSION['oc_home']['group']) {
            case '6':
                $fy = M('core_unit')->where('pid='.$_SESSION['oc_home']['unit'])->getField('id',true);
                //查总园下面所有分园
                if(I('unit')){  
                    $map['unit'] = I('unit');
                    $this->assign('unit',I('unit'));
                }else{
                    $map['unit'] = array('in',implode(',',$fy));
                }
                if(I('class_list')){  
                    $map['class_id'] = I('class_list');
                    $this->assign('class_list',I('class_list'));
                }
                
                $due = M('core_attendance')->where($map)->sum('due_num');
                $miss = M('core_attendance')->where($map)->sum('miss_num');
                //查班级用
                $_SESSION['oc_home']['fy'] = implode(',',$fy);
                break;
            case '3':
                $map['unit'] = $_SESSION['oc_home']['unit'];
                if(I('class')){  
                    $map['class_id'] = I('class');
                    $this->assign('class',I('class'));
                }
                $due = M('core_attendance')->where($map)->sum('due_num');
                $miss = M('core_attendance')->where($map)->sum('miss_num');
                //查班级用
                $_SESSION['oc_home']['fy'] = $_SESSION['oc_home']['unit'];
                break;                
        }
        
        //查班级
        $con['unit'] = array('in',$_SESSION['oc_home']['fy']);
        $classList = M('core_class')->where($con)->field('id,title')->select();
        // $classList = M('core_class')->where($con)->getField('title',true);
        //查分园
        $con2['id'] = array('in',$_SESSION['oc_home']['fy']);
        $unitList = M('core_unit')->where($con2)->field('id,title')->select();
        // $unitList = M('core_unit')->where($con2)->getField('title',true);
        $due  = ($due)?$due:0;
        $miss = ($miss)?$miss:0;
        $count=count($list);
        $this->assign('due',$due);
        $this->assign('miss',$miss);
        $this->assign('shidao',$due-$miss);
        $this->assign('classList',$classList);
        $this->assign('unitList',$unitList);
        $this->assign('count',$count);
        $this->display(); 
    }

}
